//
//  RadioViewController.m
//  Leisure
//
//  Created by xalo on 16/6/30.
//  Copyright © 2016年 朱旭东. All rights reserved.
//

#import "RadioViewController.h"

@interface RadioViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property(nonatomic,strong)NSMutableArray *dataSourceArray;
@property(nonatomic,strong) NSMutableArray *scrollViewArray;
@end

@implementation RadioViewController

- (NSMutableArray *)dataSourceArray
{
    if (!_dataSourceArray) {
        _dataSourceArray = [NSMutableArray array] ;
    }
    return _dataSourceArray;
}
- (NSMutableArray *)scrollViewArray
{
    if (!_scrollViewArray) {
        _scrollViewArray =[NSMutableArray array] ;
    }
    return _scrollViewArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets  = NO;

    [self.tableView  registerNib:[UINib  nibWithNibName:@"RadioCell" bundle:nil] forCellReuseIdentifier:@"Radio_cell"];
    
    //数据请求
    [RequestManager fetchDataWithURL:kRadioUrl parameters:@{} method:@"POST" complet:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];


        for (NSDictionary *subDic in dic[@"data"][@"carousel"]) {
            [self.scrollViewArray addObject:subDic[@"img"]];
            
        }
        for (NSDictionary *smallDic in dic[@"data"][@"alllist"]) {
            RadioModel *model = [[RadioModel alloc] init];
            [model  setValuesForKeysWithDictionary:smallDic];
            [self.dataSourceArray  addObject:model];

        }
        NSLog(@"%@",self.dataSourceArray);

        
    } updataUI:^{
        [self.tableView reloadData];
       
        //创建轮播图
        AdvertisingScrollView *headerView = [[AdvertisingScrollView alloc]initWithMinFrame:CGRectMake(0, 64, kwidth, kheight/3-64)];
        [headerView  initializationUIWithDataArray:self.scrollViewArray];
        [headerView setAnimationDuration:2];
        self.tableView.tableHeaderView = headerView;
    }];

   }

#pragma mark ------------------TableViewDelegate------------

- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSourceArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    RadioCell *cell = [tableView  dequeueReusableCellWithIdentifier:@"Radio_cell" forIndexPath:indexPath];
     RadioModel *model = self.dataSourceArray[indexPath.row];
    [cell  setCellWithModel:model];
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 150;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    RadioListViewController  *listVC  = [[RadioListViewController  alloc] initWithNibName:@"RadioListViewController" bundle:nil];
    listVC.radioid = [self.dataSourceArray[indexPath.row] radioid];
    
    
    [self.navigationController pushViewController:listVC animated:YES];

    
}
@end
